package com.wb.adutils.ads.gmad

import android.app.Activity
import android.view.ViewGroup
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.reward.RewardItem
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAd
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAdListener
import com.bytedance.msdk.api.v2.ad.interstitialFull.GMInterstitialFullAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil
import com.bytedance.msdk.api.v2.slot.GMAdSlotInterstitialFull
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.AdUtils
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants
import com.wb.adutils.tools.LogC
import java.lang.Exception


class GMInterstitialAdUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity)  {

    private var mInterstitialFullAd: GMInterstitialFullAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            if (isAdLoadSuccess && mInterstitialFullAd?.isReady == true) {
                listener?.onAdLoaded(null)
                return
            }

            isAdLoadSuccess = false
            LogC.e("插屏 -> 开始请求插屏广告")
            // 重置标签
            val adSlotInterstitialFull = GMAdSlotInterstitialFull.Builder()
                .setGMAdSlotBaiduOption(GMAdOptionUtil.getGMAdSlotBaiduOption().build())
                .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                .setBidNotify(true)
                .setUserID(AdUtils.getDeviceId(activity))//用户id,必传参数 (插全屏类型下_全屏广告使用)
                .setOrientation(orientation) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL; (插全屏类型下_全屏广告使用)
                .build()
            mInterstitialFullAd = GMInterstitialFullAd(activity, adEntity?.adUnit)
            mInterstitialFullAd?.loadAd(adSlotInterstitialFull, object : GMInterstitialFullAdLoadCallback {
                override fun onInterstitialFullLoadFail(adError: AdError) {
                    LogC.e("插屏 -> onInterstitialFullLoadFail: " + adError.message)
                    excAdLoadFail(adError.code)
                }

                override fun onInterstitialFullAdLoad() {
                }

                override fun onInterstitialFullCached() {
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("插屏 -> onInterstitialFullCached...但是已超时")
                        return
                    }
                    LogC.e("插屏 -> onInterstitialFullCached")
                    timeoutDisposable?.dispose()
                    isAdLoadSuccess = true
                    listener?.onAdLoaded(null)
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun showAd(adRoot: ViewGroup?) {
        try {
            val activity = getActivity()
            if (activity == null) {
                release()
                return
            }
            if (isAdLoadSuccess && mInterstitialFullAd?.isReady == true) {
                //设置监听器
                mInterstitialFullAd?.setAdInterstitialFullListener(object : GMInterstitialFullAdListener {
                    private var isRewarded = false
                    override fun onInterstitialFullShow() {
                        LogC.e("插屏 -> showAd -> onInterstitialFullShow...")
                        listener?.onAdDisplay()
                    }

                    override fun onInterstitialFullShowFail(adError: AdError) {
                        LogC.e("插屏 -> showAd -> onInterstitialFullShowFail: " + adError.message)
                        listener?.onAdLoadFail(adError.code)
                        release()
                    }

                    override fun onInterstitialFullClick() {
                        LogC.e("插屏 -> showAd -> onInterstitialFullClick...")
                        listener?.onAdClicked()
                    }

                    override fun onInterstitialFullClosed() {
                        LogC.e("插屏 -> showAd -> onInterstitialFullClosed...")
                        listener?.onAdComplete(isRewarded)
                        release()
                    }

                    override fun onVideoComplete() {
                    }

                    override fun onVideoError() {
                        LogC.e("插屏 -> showAd -> onVideoError...")
                    }

                    override fun onSkippedVideo() {
                        LogC.e("插屏 -> showAd -> onSkippedVideo...")
                    }

                    override fun onAdOpened() {
                    }

                    override fun onAdLeftApplication() {
                    }

                    override fun onRewardVerify(p0: RewardItem) {
                        LogC.e("插屏 -> showAd -> onRewardVerify...")
                        isRewarded = true
                    }
                })
                mInterstitialFullAd?.showAd(activity)
            } else {
                LogC.e("插屏 -> showAd ->  没有准备好")
                listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
                release()
            }
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun release() {
        super.release()
        mInterstitialFullAd?.destroy()
        mInterstitialFullAd = null
    }


}